package com.xingkeduo.pojo.json;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.enums.UseStatus;
import com.xingkeduo.pojo.query.BoardQuery;
import com.xingkeduo.pojo.table.BoardTable;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.RegularUtil;

import java.text.DateFormat;
import java.util.Iterator;
import java.util.List;

public class BoardJson extends BaseJson {

	public static BoardQuery createQueryForBoard(String json) throws Throwable {
		BoardQuery query = new BoardQuery();
		JsonNode root = validJson(json);
		if (root == null)
			return query;
		query.setCode(validCode(root, YesNoEnum.YES));
		return query;
	}


	public static BoardTable createTableForModifyBoard(String urlParam, BoardTable boardTable) throws Throwable {
		JsonNode root = validJson(urlParam);
		if (root == null)
			throw new InputParamException(InputParamException.Type.INVALID, "参数");
		boardTable.setCode(validCode(root, YesNoEnum.YES));

		return boardTable;
	}

	public static ObjectNode toJson(BoardTable boardTable) {
		ObjectNode root = MAPPER.createObjectNode();

		if (boardTable != null)
		{
			root.put("code", boardTable.getCode());
			root.put("name", boardTable.getName());
			root.put("url", boardTable.getUrl());
			root.put("isvable", boardTable.getIsvable());
			root.put("sort", boardTable.getSort());

			//处理时间
			String createdTime = DateFormat.getDateTimeInstance().format(boardTable.getCreatedTime());//可以精确到时分秒
			root.put("createdtime", createdTime);
		}

		return root;
	}

	public static ArrayNode toJson(Page<BoardTable> page) throws Throwable {
		ArrayNode root = MAPPER.createArrayNode();
		return root.add(JSON.toJSONString(page));
	}
}
